package org.example.medical.controller;

import org.example.medical.entity.SurgeryRecord;
import org.example.medical.service.SurgeryRecordService;
import org.example.medical.utils.ApiResult;
import org.example.medical.utils.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

@RestController
@RequestMapping("/api/surgeryRecords")
public class SurgeryRecordApiController {
    @Autowired
    private SurgeryRecordService surgeryRecordService;

    // 1. 分页查询接口
    @GetMapping
    public ApiResult<PageResult<SurgeryRecord>> listSurgeryRecords(
            @RequestParam(required = false) String patientName,
            @RequestParam(required = false) String doctorName,
            @RequestParam(required = false) String status,
            @RequestParam(required = false) String scheduledDate,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size
    ) {
        List<SurgeryRecord> records = surgeryRecordService.getSurgeryRecords(
                patientName,
                doctorName,
                status,
                scheduledDate != null ? LocalDate.parse(scheduledDate) : null,
                page,
                size
        );
        Long total = (long) records.size();
        return ApiResult.success(new PageResult<>(records, total, page, size));
    }

    // 2. 新增手术记录
    @PostMapping
    public ApiResult<?> addSurgeryRecord(@RequestBody SurgeryRecord surgeryRecord) {
        surgeryRecordService.addSurgeryRecord(surgeryRecord);
        return ApiResult.success(null, "Surgery record added successfully");
    }

    // 3. 更新手术记录
    @PutMapping("/{surgeryId}")
    public ApiResult<?> updateSurgeryRecord(
            @PathVariable Long surgeryId,
            @RequestBody SurgeryRecord surgeryRecord
    ) {
        surgeryRecord.setSurgeryId(surgeryId);
        surgeryRecordService.updateSurgeryRecord(surgeryRecord);
        return ApiResult.success(null, "Surgery record updated successfully");
    }

    // 4. 软删除手术记录
    @DeleteMapping("/{surgeryId}")
    public ApiResult<?> softDeleteSurgeryRecord(@PathVariable Long surgeryId) {
        surgeryRecordService.softDeleteSurgeryRecord(surgeryId);
        return ApiResult.success(null, "Surgery record deleted successfully");
    }

    // 5. 批量软删除手术记录
    @PostMapping("/batchDelete")
    public ApiResult<?> batchSoftDeleteSurgeryRecords(@RequestBody List<Long> surgeryIds) {
        surgeryRecordService.batchSoftDeleteSurgeryRecords(surgeryIds);
        return ApiResult.success(null, "Surgery records deleted successfully");
    }
}